package com.com.cgq.data.exchenge;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.example.cgq.business.R;

import java.util.List;

/**
 * Created by cgq on 2015/11/8.
 */


public class ListDataAdapter extends BaseAdapter implements AbsListView.OnScrollListener{
    private List<ListDataMember> mList;
    private LayoutInflater mInflater;
    private imageLoader mimageLoader;
    private int mStart,mEnd;
    public static String[] URLS;
    private boolean mFirstIn;
    public ListDataAdapter(Context context,List<ListDataMember> data,ListView listView){
        mList=data;
        mInflater=LayoutInflater.from(context);
        mimageLoader=new imageLoader(listView);
        URLS=new String[data.size()];
        for (int i = 0; i < data.size(); i++) {
            URLS[i]=data.get(i).strFoodPic;
        }
        mFirstIn = true;
        listView.setOnScrollListener(this);
    }
    public int getCount() {
        return mList.size();
    }
    public Object getItem(int position) {
        return mList.get(position);
    }
    public long getItemId(int position) {
        return position;
    }

    @SuppressLint("InflateParams")
    @Override
    public View getView(int position, View v, ViewGroup arg2) {
        viewHolder vHolder=null;
        if (v==null) {
            vHolder=new viewHolder();
            v=mInflater.inflate(R.layout.content_list, null);
            vHolder.FoodPic=(ImageView) v.findViewById(R.id.id_FoodPic_Left);
            vHolder.FoodName=(TextView) v.findViewById(R.id.id_FoodName_Left);
            vHolder.FoodScore=(TextView) v.findViewById(R.id.text_FoodScore_Left);
            v.setTag(vHolder);
        }else {
            vHolder=(viewHolder) v.getTag();
        }
        vHolder.FoodPic.setImageResource(R.mipmap.icon);
        String url=mList.get(position).strFoodPic;
        vHolder.FoodPic.setTag(url);

//		new imageLoader().showimageThread(vHolder.Icon, url);

//		new imageLoader().showImageByAsyncTask(vHolder.Icon, url);
        mimageLoader.showImageByAsyncTask(vHolder.FoodPic, url);
        vHolder.FoodName.setText(mList.get(position).StrFoodName);
        vHolder.FoodScore.setText(mList.get(position).strFoodScore);
        return v;
    }
    class viewHolder{
        public TextView FoodName,FoodScore;
        public ImageView FoodPic;
    }
    @Override
    public void onScroll(AbsListView view,int firstVisibleItem,int visibleItemCount,int endVisibleItem) {
        mStart=firstVisibleItem;
        mEnd=firstVisibleItem+visibleItemCount;

        if (mFirstIn && visibleItemCount>0) {
            mimageLoader.loadImages(mStart,mEnd);
            mFirstIn=false;
        }
    }
    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {
        if (scrollState==SCROLL_STATE_IDLE) {

            mimageLoader.loadImages(mStart,mEnd);
        }else {
            mimageLoader.cancelAllTasks();
        }
    }
}